package TP6;

import anaydis.search.SymbolTable;
import anaydis.search.text.TextIndex;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: pc
 * Date: 18/11/11
 * Time: 21:13
 * To change this template use File | Settings | File Templates.
 */
public class TextIndexImpl implements TextIndex{

    private SymbolTable<String, List<Integer>> table;
    private int n;

    public TextIndexImpl(SymbolTable<String, List<Integer>> symbolTable) {
        table = symbolTable;
    }

    public List<Integer> search(String s){
        return table.get(s);
    }

    public void setN(int i){
        n = i;
    }

    public void buildIndex(String s){
        try{
            FileReader reader = new FileReader(s);
            int words = 0;
            while(words < n){
                String string = new String();
                int i = reader.read();
                while(((65 <= i) && (i <= 90))||((97 <= i) && (i <= 122))){
                    string = string.concat(new Character((char) i).toString());  //Yay for unchecked exceptions~
                    i = reader.read();
                }
                if(!string.equals("") && !table.containsKey(string)){
                    List<Integer> list = new ArrayList<Integer>();
                    list.add(words);
                    table.put(string, list);
                    words++;
                }else if(!string.equals("")){
                    List<Integer> list = table.get(string);
                    list.add(words);
                    table.put(string, list);
                }
            }
        }catch(IOException e){
            System.out.println(e.getMessage());
        }
    }

}
